#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

import os
import configparser
import binascii

from common.libs.ComDeal import WATER_CMD
from common.libs.ComDeal import ComDeal

from ui.envMeasure import Ui_Dialog

class EnvMeasureDialog(QDialog, Ui_Dialog):

    signal_send = pyqtSignal(str)

    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.setupUi(self)
        self.setWindowIcon(QIcon(':icons/img/box.png'))
        self.setWindowTitle("本底测量")
        # 配置文件读取
        self.period = 200
        # 串口模块
        self.pushButton_start.clicked.connect(self.start)
        self.progressBar.setRange(0, 100)
        self.progressBar.setValue(0)

    def start(self):
        '''开始本底测量'''
        cmd = ComDeal.getCmd(WATER_CMD['本底测量'])
        self.signal_send.emit(cmd)

    def serialDeal(self, data):
        t = int(data, 16) * self.period / 1000
        t1 = float('%0.1f' % (100 - t * 100 / 30))
        self.progressBar.setValue(t1)
        if t == 0:
            QMessageBox.information(self, '提示', '测量完成', QMessageBox.Yes)
            self.progressBar.setValue(0)
            self.close()

